package com.job.client.company.listener;

import com.alibaba.fastjson2.JSON;
import com.job.admin.model.audit.constant.StatusConstant;
import com.job.admin.model.audit.pojo.AuditCompany;
import com.job.client.common.exception.MyselfException;
import com.job.client.common.result.ResponseResult;
import com.job.client.company.mapper.AuthenticationMapper;
import com.job.client.company.mapper.CompanyMapper;
import com.job.client.model.auth.pojo.User;
import com.job.client.model.company.constant.EmployeeStatusConstant;
import com.job.client.model.company.pojo.Authentication;
import com.job.client.model.company.pojo.Company;
import com.job.client.model.rabbitmq.AuditResultConstant;
import com.job.client.model.rabbitmq.MessageConstant;
import com.job.client.model.rabbitmq.MysqlToEsGDSNConstant;
import com.job.feign.admin.audit.AuditFeign;
import com.job.feign.client.auth.AuthFeign;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.UUID;

@Component
public class CompanyListener {
    @Autowired
    private AuditFeign auditFeign;

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private AuthenticationMapper authenticationMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private AuthFeign authFeign;

    @RabbitListener(queues = AuditResultConstant.QUEUE_AUDIT_COMPANY_RESULT)
    public void setCompany(String dataJSON){
        HashMap<String,String> data = JSON.parseObject(dataJSON, HashMap.class);
        String auditCompanyId = data.get("audit_company_id");
        ResponseResult result = auditFeign.getCompanyResultById(auditCompanyId);
        if(result.getCode()!=200){
            new MyselfException(201,"接收头像审核结果的数据报错",null);
            return;
        }
        AuditCompany auditCompany = JSON.parseObject(JSON.toJSONString(result.getData()), AuditCompany.class);

        // 整合rabbitmq发送的数据
        HashMap<String,String> message = new HashMap<>();
        message.put("userId",auditCompany.getUserId()); // 用户的id
        message.put("result",data.get("result")); // 是否审核通过

        // 添加到数据库操作
        if(StatusConstant.STATUS_AUDIT_SUCCESS.equals(Integer.valueOf(data.get("result")))){
            Company company= new Company();
            company.setName(auditCompany.getCompanyName());
            company.setCipheKey(UUID.randomUUID().toString());
            int i1 = companyMapper.insert(company);
            if(i1 != 1){
                new MyselfException(201,"接收公司审核结果的插入到company数据失败",null);
                return;
            }
            String companyId = company.getCompanyId();
            String userId = auditCompany.getUserId();
            Authentication authentication = new Authentication(companyId,userId, EmployeeStatusConstant.BOSS);
            int i2 = authenticationMapper.insert(authentication);
            if(i2 != 1){
                new MyselfException(201,"接收公司审核结果的插入到authentication数据失败",null);
                return;
            }
            ResponseResult responseResult = authFeign.authentication(companyId, userId,auditCompany.getCompanyName());
            if(responseResult.getCode() != 200){
                new MyselfException(201,"用户公司认证失败",null);
            }
        }else{
            message.put("reason",auditCompany.getReason()); // 审核未通过的理由
        }
        // 发送rabbitmq
        rabbitTemplate.convertAndSend(
                MessageConstant.EXCHANGE_MESSAGE,
                MessageConstant.ROUTINGKEY_COMPANY,
                JSON.toJSONString(message));

    }
}
